Shader "TideShader/Phong光照模型"
{
    Properties
    {
        _Specular("Specular",color) = (1,1,1,1)
        _Gloss("Gloss",Range(8,256)) = 20
    }
    SubShader
    {
        Tags
        {
            "LightMode" = "ForwardBase"
            "Queue" = "Geometry"
            "RenderType"="Opaque"
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float3 normal : TEXCOORD0;
                float3 view : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            float4 _Specular;
            float _Gloss;


            ///Phong光照模型公式
            half3 PhongLight(half3 n, half3 v, half3 l, half3 gloss)
            {
                half3 reflDir = normalize(reflect(-l, n));
                return pow(saturate(dot(reflDir, v)), gloss);
            }

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.normal = UnityObjectToWorldNormal(v.normal);
                o.view = UnityWorldSpaceViewDir(mul(unity_ObjectToWorld, v.vertex));
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float3 worldNormal = normalize(i.normal);
                float3 worldLightDir = normalize(_WorldSpaceLightPos0);
                fixed3 viewDir = normalize(i.view);
                float3 lightColor = _LightColor0.rgb;
                half3 specular = PhongLight(worldNormal, viewDir, worldLightDir, _Gloss);
                specular = specular * lightColor * _Specular;

                return specular.xyzx;
            }
            ENDCG
        }
    }
}